package com.dindang.propertyfee.mvp.ui.adapter.custom;

import android.support.annotation.NonNull;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dindang.propertyfee.R;
import com.dindang.propertyfee.app.utils.TextViewUtils;
import com.dindang.propertyfee.mvp.model.entity.PropertyCharge;

import java.util.List;

/**应收明细适配器*/
public class ReceivableDetailViewAdapter extends RecyclerView.Adapter<ReceivableDetailViewAdapter.ViewHolder> {
    List<PropertyCharge.NodedataDetail.Bills> list;
    OnReceivableDetailListener listener;
    public ReceivableDetailViewAdapter(List<PropertyCharge.NodedataDetail.Bills> list, OnReceivableDetailListener listener) {
        this.list = list;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.propertyfee_item_receivable_detail_cost, parent, false);
        ReceivableDetailViewAdapter.ViewHolder viewHolder = new ReceivableDetailViewAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        PropertyCharge.NodedataDetail.Bills costItem = list.get(position);
        holder.tvCostItem.setText(costItem.c_fee_name);
        holder.tvCost.setText(TextViewUtils.getAmount2(costItem.c_uncollected_amount));
        if(costItem.isDetailVisibility) {
            holder.view_detail.setVisibility(View.VISIBLE);
            holder.ivArrow.setImageDrawable(holder.ivArrow.getContext().getDrawable(R.mipmap.propertyfee_ic_arrow_up2));
        }else{
            holder.view_detail.setVisibility(View.GONE);
            holder.ivArrow.setImageDrawable(holder.ivArrow.getContext().getDrawable(R.mipmap.propertyfee_ic_arrow_down2));
        }
        if(costItem.overdueDays < 0){
            holder.tvOverdue.setText("逾期");
            holder.tvOverdue.setVisibility(View.VISIBLE);
            holder.tvOverdue.setBackgroundResource(R.drawable.property_bg_radio_10_red);
        }else if(costItem.overdueDays == 0){
            holder.tvOverdue.setText("今日到期");
            holder.tvOverdue.setVisibility(View.VISIBLE);
            holder.tvOverdue.setBackgroundResource(R.drawable.property_bg_radio_10_blue);
        }else if(costItem.overdueDays == 1){
            holder.tvOverdue.setText("明日到期");
            holder.tvOverdue.setVisibility(View.VISIBLE);
            holder.tvOverdue.setBackgroundResource(R.drawable.property_bg_radio_10_blue);
        }else if(costItem.overdueDays <= 7 && costItem.overdueDays >1){
            holder.tvOverdue.setText("7日到期");
            holder.tvOverdue.setVisibility(View.VISIBLE);
            holder.tvOverdue.setBackgroundResource(R.drawable.property_bg_radio_10_blue);
        }else if(costItem.overdueDays <= 30 && costItem.overdueDays >7){
            holder.tvOverdue.setText("30日到期");
            holder.tvOverdue.setVisibility(View.VISIBLE);
            holder.tvOverdue.setBackgroundResource(R.drawable.property_bg_radio_10_blue);
        }else{
            holder.tvOverdue.setVisibility(View.GONE);
        }
        holder.tvDosage.setText("用量: " + costItem.c_quantity);
        holder.tvNewRead.setText("本次读数: " + costItem.c_this_time_num);
        holder.tvOldRead.setText("上次读数: " + costItem.c_last_time_num);
        holder.tvUnitPrice.setText("应收对象: " + costItem.c_name);
        holder.tvDataInterval.setText("应收日期: " + costItem.d_modify_billing_date);
        holder.tvReceivableDate.setText("费用期间: " + costItem.d_modify_billing_date + "-" + costItem.d_end_billing_date);
        holder.clCostItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(costItem.isDetailVisibility){
                    costItem.isDetailVisibility = false;
                }else{
                    costItem.isDetailVisibility = true;
                }
                notifyItemChanged(position);
            }
        });

        if(costItem.isCheck) {
            holder.aSwitch.setImageDrawable(holder.ivArrow.getContext().getDrawable(R.mipmap.propertyfee_ic_select_press));
        }else{
            holder.aSwitch.setImageDrawable(holder.ivArrow.getContext().getDrawable(R.mipmap.propertyfee_ic_select_normal));
        }

        holder.aSwitch.setOnClickListener(view -> listener.onClick(position));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout clCostItem;
        TextView tvCostItem;
        TextView tvOverdue;
        TextView tvCost;
        ImageView aSwitch;
        TextView tvReceivableDate;
        ConstraintLayout view_detail;
        ImageView ivArrow;

        TextView tvDosage;
        TextView tvNewRead;
        TextView tvOldRead;
        TextView tvUnitPrice;
        TextView tvDataInterval;
        ViewHolder(View itemView) {
            super(itemView);
            clCostItem = itemView.findViewById(R.id.propertyfee_cl_cost_item);
            tvCostItem = itemView.findViewById(R.id.propertyfee_tv_cost_item);
            tvOverdue = itemView.findViewById(R.id.propertyfee_tv_overdue);
            tvCost = itemView.findViewById(R.id.propertyfee_tv_cost);
            aSwitch = itemView.findViewById(R.id.propertyfee_switchChecked);
            tvReceivableDate = itemView.findViewById(R.id.propertyfee_tv_receivable_date);
            ivArrow = itemView.findViewById(R.id.propertyfee_iv_arrow);
            view_detail = itemView.findViewById(R.id.propertyfee_view_detail);

            tvDosage = itemView.findViewById(R.id.propertyfee_tv_dosage);
            tvNewRead = itemView.findViewById(R.id.propertyfee_tv_new_read);
            tvOldRead = itemView.findViewById(R.id.propertyfee_tv_old_read);
            tvUnitPrice = itemView.findViewById(R.id.propertyfee_tv_unit_price);
            tvDataInterval = itemView.findViewById(R.id.propertyfee_tv_data_interval);
        }
    }

    public interface OnReceivableDetailListener{
        void onClick(int position);
    }

}